import React from 'react';
import { Tabs } from 'antd';
import router from 'umi/router';
import _ from 'lodash';
import * as api from '@/services/ReturnMoneyPlanMgr';

export default function CurrentStage(props) {
  let activeKey = '';
  const { orgStructure, match } = props;
  const { level1, level2, project, stage } = match.params;
  let stages = {};
  if (!_.isEmpty(orgStructure.data)) {
    stages = _.filter(orgStructure.data, {
      parentOrgid: project,
    });
  }
  stages = _.orderBy(stages, ['orgorder'], ['asc']);

  if (props.location.pathname.search('EntireProject') === -1) {
    if (stage) {
      activeKey = stage;
    } else {
      activeKey = stages[0].orgdepid;
    }
  } else {
    activeKey = 'EntireProject';
  }

  if (!_.isEmpty(orgStructure.data) && project) {
    return (
      <Tabs
        size="large"
        activeKey={activeKey}
        animated={false}
        defaultActiveKey="1"
        onChange={key => {
          if (key === 'EntireProject') {
            router.push(
              `/OperatingPlan/ReturnMoneyPlan/EntireProject/${level1}/${level2}/${project}`,
            );
          } else {
            // dynamic 月度动态版
            // 根据当前分期最新版本信息 跳转分期最新的版本 默认跳转  CastDecision 版本
            const params = { stageId: key };
            api.newVersionTypeUsingGET({ params }).then(data => {
              if (data.data && data.data !== 'pricing') {
                router.replace(
                  `/OperatingPlan/ReturnMoneyPlan/${level1}/${level2}/${project}/${key}/${data.data}`,
                );
              } else {
                router.replace(
                  `/OperatingPlan/ReturnMoneyPlan/${level1}/${level2}/${project}/${key}/investment`,
                );
              }
            });
          }
        }}
      >
        <Tabs.TabPane tab="全盘回款计划" key="EntireProject" />
        {!_.isEmpty(stages) &&
          stages.map(value => <Tabs.TabPane tab={value.orgname} key={value.orgdepid} />)}
      </Tabs>
    );
  }
  return null;
}
